/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package GameLogic;

import java.util.*;
import java.io.*;
import GameLogic.Tiles.*;
import Enums.*;
import org.lwjgl.*;
import org.lwjgl.util.*;

/**
 *
 * @author Falc
 */

public class Map
{
    private Vector<Vector<Tile>> tiles;
    private Vector<Dimension> spawns;

    private int mapWidth;
    private int mapHeight;

    public Map()
    {
        mapWidth = 0;
        mapHeight = 0;
        tiles = new Vector<Vector<Tile>>();
        spawns = new Vector<Dimension>();
    }



    public boolean loadMap( String mapname ) // schreibt in einen TileVektor
    {
        tiles.clear();
        spawns.clear();
        mapWidth = mapHeight = 0;
        int y = 0 , x = 0;
        int curTile = 47;
        try
        {
            BufferedReader reader = new BufferedReader( new FileReader( "maps/" + mapname + ".map" ) );

            curTile = reader.read();
            tiles.add( new Vector<Tile>() );

            while(  curTile != -1 ) // EOF
            {
                if ( curTile == 13 ) // \n
                {
                    y++;
                    x = 0;
                    tiles.add( new Vector<Tile>() );
                }
                else if ( curTile == 10 ) // \r
                {
                }
                else
                {
                    switch( curTile )
                    {
                        case (int)'0':
                            tiles.elementAt(y).add( new None() );
                            break;

                        case (int)'1':
                            tiles.elementAt(y).add( new Free() );
                            break;

                        case (int)'2':
                            tiles.elementAt(y).add( new Wall() );
                            break;

                        case (int)'3':
                            tiles.elementAt(y).add( new Free_grass() );
                            break;

                        case (int)'4':
                            tiles.elementAt(y).add( new Wall_stone() );
                            break;

                        case (int)'5':
                            tiles.elementAt(y).add( new Wall_lava() );
                            break;

                        case (int)'6':
                            tiles.elementAt(y).add( new Free_grass() );
                            spawns.add( new Dimension( y , x ) );
                            break;

                        case (int)'7':
                            tiles.elementAt(y).add( new Wall_water() );
                            break;

                        case (int)'8':
                            tiles.elementAt(y).add( new Wall_tree() );
                            break;

                        default: 
                            Sys.alert( "Error" , "Not able to load map. Unknown character! (" + curTile + " )" );
                            tiles.clear();
                            return false;
                    }
                    x++;
                }
                curTile = reader.read();
            }
        }
        catch( Exception e )
        {
            Sys.alert( "Error" , "Not able to load map. Unknown Error!" );
            e.printStackTrace();
            return false;
        }
        
        //check mapsize
        mapHeight = tiles.size();
        for( int i = 0 ; i < mapHeight ; i++ )
        {
            if( tiles.elementAt(i).size() > mapWidth )
                mapWidth = tiles.elementAt(i).size();
        }

        // fill to rectangle with Nones
        int rowLength = 0;
        for( int s = 0 ; s < mapHeight ; s++ )
        {
            rowLength = tiles.elementAt(s).size();
            for( int a = 0 ; a < rowLength ; a++ )
            {
                tiles.elementAt(s).add( new None() );
            }

        }

        return true;
    }

    public int returnWidth()
    {
        return mapWidth;
    }

    public int returnHeight()
    {
        return mapHeight;
    }

    public String returnTexture( int x , int y )
    {
        if( tiles.size() <= y || tiles.elementAt( y ).size() <= x || x < 0 || y < 0 )
            return "none.bmp";
        return tiles.elementAt(y).elementAt(x).getTexture();
    }

    public int returnEffects( int x , int y )
    {
        if( tiles.size() <= y || tiles.elementAt( y ).size() <= x || x < 0 || y < 0 )
            return Effects.DEADLY.code;
        return tiles.elementAt(y).elementAt(x).getCode();
    }
    
    public int returnID( int x , int y )
    {
        return tiles.elementAt(y).elementAt(x).getID();
    }

    public Dimension returnSpawnAt( int i )
    {
        if( i >= spawns.size() )
        {
            Sys.alert( "Error" , "No more spawns available!" );
            return null;
        }
        return spawns.elementAt( i );
    }

    public boolean isFoodable(int x , int y)
    {
        if( tiles.size() < y || tiles.elementAt( y ).size() < x || x < 0 || y < 0 )
            return false;
        return ((tiles.elementAt(y).elementAt(x).getCode() & Effects.FOODABLE.code)== Effects.FOODABLE.code);
    }
}

